package com.training.form;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;

/**
 * Created by Ywen on 2018/12/2.
 */
public class FormBeanRef {

    public static ActionForm load(String formName, HttpServletRequest request) {
        ActionForm form = null;
        try {
            Class clazz = Class.forName(formName);
            form = (ActionForm) clazz.newInstance();
            Field[] declaredFields = clazz.getDeclaredFields();
            for (Field field : declaredFields) {
                field.setAccessible(true);
                field.set(form,request.getParameter(field.getName()));
                field.setAccessible(false);
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        }
        return form;
    }
}
